package org.timeismoney.payment.web.action;

import java.io.IOException;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

/**
 * BaseAction封装了一些Helper方法.通过这些方法可以方便的保存Message,渲染输出的结果.
 * <p>
 * 例如:
 * <p>
 * 你可以在某个动作完成之后设置一个提示信息,如保存成功,没有找到对应的记录等等,通过调用
 * {@link #saveMessage(HttpServletRequest, String)}或
 * {@link #saveError(HttpServletRequest, String)} 直接保存Message或者Error,
 * <p>
 * 然后 在jsp中可通过标签 &lt; struts:message key="the key of message"/&gt; 使用Message
 */
public abstract class BaseAction extends DispatchAction {

	public static final String		SUCCESS					= "success";

	public static final String		FAILURE					= "failure";

	public static final String		ERROR					= "error";

	public static final String		DIRECTLY_MESSAGE_KEY	= "message";

	public static final String		DIRECTLY_ERROR_KEY		= "error";

	public static final String		TEXT_ERROR_MSG_ADD		= "添加失败";
	public static final String		TEXT_ERROR_MSG_EDIT		= "更新失败";
	public static final String		TEXT_ERROR_MSG_DEL		= "删除失败";
	public static final String		TEXT_ERROR_MSG_QUERY	= "查询失败";

	/*
	 * 系统错误页面
	 */
	protected final static String	ERRORPATH				= "/error.jsp";

	/*
	 * 业务错误页面
	 */
	protected final static String	BUSINESSERRORPATH		= "/businessError.jsp";

	// *******************Message助手方法****************

	/**
	 * 保存单条消息到Message的简化函数.
	 */
	protected void saveMessage(HttpServletRequest request, String key,
			String[] values) {
		ActionMessages msgs = new ActionMessages();
		msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key, values));
		saveMessages(request, msgs);
	}

	/**
	 * 直接保存文本信息(非i18n)到messages.
	 * 
	 * @param message
	 *            直接的文本信息
	 */
	protected void saveDirectlyMessage(HttpServletRequest request,
			String message) {
		ActionMessages msgs = new ActionMessages();
		msgs.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
				DIRECTLY_MESSAGE_KEY, message));
		saveMessages(request, msgs);
	}

	/**
	 * 保存单条信息到Error的简化函数.
	 */
	protected void saveError(HttpServletRequest request, String key) {
		saveError(request, key, null);
	}

	/**
	 * 保存单条信息到Error的简化函数.
	 */
	protected void saveError(HttpServletRequest request, String key,
			String[] values) {
		ActionMessages errors = new ActionMessages();
		errors.add(ActionMessages.GLOBAL_MESSAGE,
				new ActionMessage(key, values));
		saveErrors(request, errors);
	}

	/**
	 * 直接保存文本信息(非i18n)errors.
	 * 
	 * @param message
	 *            直接的文本信息
	 */
	protected void saveDirectlyError(HttpServletRequest request, String message) {
		ActionMessages errors = new ActionMessages();
		errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
				DIRECTLY_MESSAGE_KEY, message));
		saveErrors(request, errors);
	}

	// ********************渲染输出结果的展现界面*******************
	/**
	 * 直接输出.
	 * 
	 * @param contentType
	 *            内容的类型.html,text,xml,json,excel,word的值见后
	 */
	protected void render(HttpServletResponse response, String text,
			String contentType) {
		try {
			response.setCharacterEncoding("utf-8");
			response.setContentType(contentType);
			response.getOutputStream().write(text.getBytes("utf-8"));
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
	}

	/**
	 * 直接输出J-SON代码.在B/S之间直接传递JSON代码是Ajax中一种比较实效的用法,JSON的定义如{name:"Tom",age:20}
	 */
	protected void renderJson(HttpServletResponse response, String text) {
		render(response, text, "text/x-json;charset=utf-8");
	}

	/**
	 * 直接输出纯文本.
	 */
	protected void renderText(HttpServletResponse response, String text) {
		render(response, text, "text/plain;charset=utf-8");
	}

	/**
	 * 直接输出HTML.
	 */
	protected void renderHtml(HttpServletResponse response, String text) {
		render(response, text, "text/html;charset=utf-8");
	}

	/**
	 * 直接输出XML.
	 */
	protected void renderXML(HttpServletResponse response, String text) {
		render(response, text, "text/xml;charset=utf-8");
	}

	/**
	 * 直接输出Excel.
	 */
	protected void renderExcel(HttpServletResponse response, String text) {
		render(response, text, "application/vnd.ms-excel;charset=utf-8");
	}

	/**
	 * 直接输出Word.
	 */
	protected void renderWord(HttpServletResponse response, String text) {
		render(response, text, "application/vnd.ms-word;charset=utf-8");
	}

	/**
	 * 直接输出Pdf.
	 */
	protected void renderPdf(HttpServletResponse response, String text) {
		render(response, text, "application/pdf;charset=utf-8");
	}

	/**
	 * 创建一个新的redirect为true的actionForward.
	 */
	public ActionForward createParameterActionForward(String path) {
		ActionForward actionForward = new ActionForward();
		actionForward.setPath(path);
		actionForward.setRedirect(true);
		return actionForward;
	}

	public void flushSession(HttpSession session, Object obj, String attrKey) {
		session.setAttribute(attrKey, obj);
	}

	public void buildModel(ActionForm form, Serializable s) {
		try {
			BeanUtils.copyProperties(s, form);
		} catch (Exception e) {
		}
	}

}
